package com.elite.groovy.dev;

import com.elite.groovy.kh.KHExcelUtil;
import com.elite.groovy.kh.KHUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;

/**
 * @projectName: ngsapitask-davco
 * @package: com.elite.groovy.kh
 * @className: KHExcelDev
 * @description: TODO
 * @author: LiJT
 * @date: 2023-11-24 15:49
 * @version: 1.0
 */
public class KHExcelChatDev {
    private static final Log log = LogFactory.getLog(KHExcelChatDev.class);

    public static void main(String[] args) {

        JSONObject callJson = JSONObject.fromObject("{\"export_type\":\"CHATHIS\",\"data\":[[11719,\"2000\",\"Tao\",\"张三\",\"15000000000\",\"2024-01-16 14:48:08\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-16 14:48:08\",\"2024-01-16 14:48:51\",\"00:00:43\",11,927,\"\",\"\",\"\",\"2024-01-16 14:48:33\",\"默认队列(勿删)\",\"5星\",\"\",\"\",\"\",\"\",\"防伪查询\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"000001\",\"00:00:02\",\"0\",\"满意度申诉\",\"\",1,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11718,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-16 14:44:59\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-16 14:44:59\",\"2024-01-16 14:48:07\",\"00:03:07\",9,924,\"\",\"\",\"\",\"2024-01-16 14:46:05\",\"默认队列(勿删)\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",1,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11717,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-16 14:42:21\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-16 14:42:21\",\"2024-01-16 14:44:58\",\"00:02:36\",17,2054,\"\",\"\",\"\",\"2024-01-16 14:43:52\",\"默认队列(勿删)\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",1,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11716,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-16 14:25:00\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-16 14:25:00\",\"2024-01-16 14:42:20\",\"00:17:20\",5,382,\"\",\"\",\"\",\"2024-01-16 14:25:50\",\"默认队列(勿删)\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",1,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11715,\"\",\"张晓彤\",\"张三\",\"15000000000\",\"2024-01-16 14:12:35\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-16 14:12:35\",\"2024-01-16 14:24:58\",\"00:12:23\",14,1125,\"\",\"\",\"\",\"2024-01-16 14:13:07\",\"默认队列(勿删)\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"WDJQZ8\",\"00:00:09\",\"0\",\"满意度申诉\",\"\",1,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11713,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-16 11:03:59\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-16 11:03:59\",\"2024-01-16 11:48:03\",\"00:44:04\",15,1743,\"\",\"\",\"\",\"2024-01-16 11:05:34\",\"默认队列(勿删)\",\"\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",1,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11712,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-15 21:47:43\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-15 21:47:43\",\"2024-01-15 22:18:03\",\"00:30:20\",7,787,\"\",\"\",\"\",\"2024-01-15 21:47:47\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",0,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11711,\"\",\"张晓彤\",\"张三\",\"15000000000\",\"2024-01-15 21:32:01\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-15 21:32:01\",\"2024-01-15 21:47:43\",\"00:15:41\",20,1026,\"\",\"\",\"\",\"2024-01-15 21:39:20\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"WDJQZ8\",\"00:00:02\",\"0\",\"满意度申诉\",\"\",0,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11710,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-15 21:24:00\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-15 21:24:00\",\"2024-01-15 21:32:00\",\"00:07:59\",11,1040,\"\",\"\",\"\",\"2024-01-15 21:31:52\",\"\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",0,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11709,\"\",\"张晓彤\",\"张三\",\"15000000000\",\"2024-01-15 21:20:36\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-15 21:20:36\",\"2024-01-15 21:24:00\",\"00:03:23\",17,844,\"\",\"\",\"\",\"2024-01-15 21:21:57\",\"\",\"\",\"\",\"\",\"\",\"\",\"扫码红包\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"WDJQZ8\",\"00:00:03\",\"0\",\"满意度申诉\",\"\",0,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11708,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-15 21:19:48\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-15 21:19:48\",\"2024-01-15 21:20:32\",\"00:00:43\",7,787,\"\",\"\",\"\",\"2024-01-15 21:19:53\",\"\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",0,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11707,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-15 21:07:41\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-15 21:07:41\",\"2024-01-15 21:19:47\",\"00:12:06\",13,1162,\"\",\"\",\"\",\"2024-01-15 21:19:25\",\"\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",0,\"183.14.133.205\",\"张三\",\"15000000000\",\"\",\"\"],[11705,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-08 16:36:23\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-08 16:36:23\",\"2024-01-08 17:17:22\",\"00:40:58\",5,763,\"\",\"\",\"\",\"2024-01-08 16:36:40\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",0,\"119.130.115.212\",\"张三\",\"15000000000\",\"\",\"\"],[11704,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-08 16:33:28\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-08 16:33:28\",\"2024-01-08 16:36:22\",\"00:02:53\",5,723,\"\",\"\",\"\",\"2024-01-08 16:33:37\",\"\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",0,\"119.130.115.212\",\"张三\",\"15000000000\",\"\",\"\"],[11703,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-08 16:31:25\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-08 16:31:25\",\"2024-01-08 16:33:26\",\"00:02:01\",5,723,\"\",\"\",\"\",\"2024-01-08 16:31:30\",\"\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",0,\"119.130.115.212\",\"张三\",\"15000000000\",\"\",\"\"],[11702,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-08 16:30:24\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-08 16:30:24\",\"2024-01-08 16:31:23\",\"00:00:59\",9,1092,\"\",\"\",\"\",\"2024-01-08 16:31:18\",\"\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",0,\"119.130.115.212\",\"张三\",\"15000000000\",\"\",\"\"],[11701,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-08 16:08:43\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-08 16:08:43\",\"2024-01-08 16:30:21\",\"00:21:38\",7,669,\"\",\"\",\"\",\"2024-01-08 16:11:20\",\"\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",0,\"119.130.115.212\",\"张三\",\"15000000000\",\"\",\"\"],[11700,\"机器人\",\"机器人\",\"张三\",\"15000000000\",\"2024-01-08 16:07:16\",\"ROBOT_ACCEPTED\",\"customer request\",\"2024-01-08 16:07:16\",\"2024-01-08 16:08:40\",\"00:01:23\",7,659,\"\",\"\",\"\",\"2024-01-08 16:07:23\",\"\",\"5星\",\"\",\"\",\"\",\"\",\"\",\"修改小结\",\"CHAT_IGS0IUS7TZVFAUWZYEXX96ULRXORYEI\",\"修改客户\",\"\",0,\"\",\"\",\"0\",\"\",\"\",0,\"119.130.115.212\",\"张三\",\"15000000000\",\"\",\"\"]],\"fields\":[{\"colName\":\"多选\",\"display\":{\"0\":{\"jQuery360037919460871224931\":{\"events\":{\"click\":[{\"type\":\"click\",\"origType\":\"click\",\"guid\":37343,\"namespace\":false},{\"type\":\"click\",\"origType\":\"click\",\"guid\":37342,\"namespace\":\"\"}]},\"click\":false}},\"length\":1},\"width\":50,\"realCol\":28,\"editable\":7},{\"colName\":\"\",\"display\":\"会话ID\",\"width\":60,\"realCol\":0},{\"colName\":\"\",\"display\":\"工号\",\"width\":80,\"realCol\":1},{\"colName\":\"\",\"display\":\"话务员名\",\"width\":100,\"realCol\":2,\"editable\":0},{\"colName\":\"\",\"display\":\"客户名\",\"width\":100,\"realCol\":3,\"editable\":0},{\"colName\":\"\",\"display\":\"来访渠道\",\"width\":0,\"realCol\":22,\"editable\":0},{\"colName\":\"\",\"display\":\"联系方式\",\"width\":100,\"realCol\":4,\"editable\":0},{\"colName\":\"\",\"display\":\"客户账号\",\"width\":0,\"realCol\":33,\"editable\":0},{\"colName\":\"\",\"display\":\"请求时间\",\"width\":140,\"realCol\":5,\"editable\":0},{\"colName\":\"\",\"display\":\"name\",\"width\":0,\"realCol\":6},{\"colName\":\"\",\"display\":\"name1\",\"width\":0,\"realCol\":7},{\"colName\":\"\",\"display\":\"聊天开始时间\",\"width\":140,\"realCol\":8,\"editable\":0},{\"colName\":\"\",\"display\":\"聊天结束时间\",\"width\":140,\"realCol\":9,\"editable\":0},{\"colName\":\"\",\"display\":\"聊天时长\",\"width\":80,\"realCol\":10},{\"colName\":\"\",\"display\":\"排队时长\",\"width\":100,\"realCol\":30,\"editable\":0},{\"colName\":\"\",\"display\":\"回复行数\",\"width\":70,\"realCol\":11},{\"colName\":\"\",\"display\":\"回复字数\",\"width\":70,\"realCol\":12},{\"colName\":\"\",\"display\":\"来源\",\"width\":0,\"realCol\":13,\"editable\":0},{\"colName\":\"\",\"display\":\"open_id\",\"width\":0,\"realCol\":14,\"editable\":0},{\"colName\":\"\",\"display\":\"昵称\",\"width\":0,\"realCol\":15,\"editable\":0},{\"colName\":\"\",\"display\":\"队列\",\"width\":80,\"realCol\":17,\"editable\":0},{\"colName\":\"\",\"display\":\"评分\",\"width\":80,\"realCol\":18,\"editable\":0},{\"colName\":\"\",\"display\":\"是否解决\",\"width\":0,\"realCol\":19,\"editable\":0},{\"colName\":\"\",\"display\":\"满意度详细\",\"width\":0,\"realCol\":20,\"editable\":0},{\"colName\":\"\",\"display\":\"满意度备注\",\"width\":100,\"realCol\":21,\"editable\":0},{\"colName\":\"\",\"display\":\"小结内容\",\"width\":120,\"realCol\":23,\"editable\":0},{\"colName\":\"\",\"display\":\"操作\",\"width\":100,\"realCol\":24,\"editable\":12},{\"colName\":\"\",\"display\":\"客户guid\",\"width\":0,\"realCol\":25,\"editable\":0},{\"colName\":\"\",\"display\":\"客户修改\",\"width\":0,\"realCol\":26,\"editable\":12},{\"colName\":\"\",\"display\":\"客户备注\",\"width\":0,\"realCol\":27,\"editable\":0},{\"colName\":\"\",\"display\":\"staff_id\",\"width\":0,\"realCol\":29,\"editable\":0},{\"colName\":\"\",\"display\":\"满意度申诉\",\"width\":0,\"realCol\":32,\"editable\":12},{\"colName\":\"\",\"display\":\"线索数\",\"width\":0,\"realCol\":31,\"editable\":0},{\"colName\":\"\",\"display\":\"队列id\",\"width\":0,\"realCol\":34,\"editable\":0},{\"colName\":\"\",\"display\":\"请求IP\",\"width\":100,\"realCol\":35,\"editable\":0},{\"colName\":\"\",\"display\":\"客服域-联系人\",\"width\":0,\"realCol\":36,\"editable\":0},{\"colName\":\"\",\"display\":\"客服域-联系电话\",\"width\":0,\"realCol\":37,\"editable\":0},{\"colName\":\"\",\"display\":\"客服域-客户昵称\",\"width\":0,\"realCol\":38,\"editable\":0},{\"colName\":\"\",\"display\":\"客服域-备注\",\"width\":0,\"realCol\":39,\"editable\":0}]}");


        JSONArray headArr = callJson.getJSONArray("fields");
        JSONArray dataArr = callJson.getJSONArray("data");

        int headNum = headArr.size();
        int dataNum = dataArr.size();

        // 表头
        Map<String, String> headMap = new HashMap<String, String>();
        List<String> headList = new ArrayList<>();
        for (int i = 0; i < headNum; i++) {
            // 表头
            String colName = KHUtil.GetSFJV(headArr.getJSONObject(i), "colName");
            String display = KHUtil.GetSFJV(headArr.getJSONObject(i), "display");
            String width = KHUtil.GetSFJV(headArr.getJSONObject(i), "width");
            String realCol = KHUtil.GetSFJV(headArr.getJSONObject(i), "realCol");
            String editable = KHUtil.GetSFJV(headArr.getJSONObject(i), "editable");
            log.debug("表头字段过滤结果 colName:" + colName + " display:" + display + " width:" + width + " realCol:" + realCol + " editable:" + editable);

            // 表头字段过滤
            if (KHUtil.isNull(colName) && !KHUtil.isNull(display) && !Objects.equals(width, "0") && (Objects.equals(editable, "0") || KHUtil.isNull(editable))) {
                headMap.put(display, realCol);
                headList.add(display);
            }
        }
        int fieldsNum = headList.size();
        log.debug("表头字段过滤结果:" + headMap);

        // 数据列
        List<LinkedHashMap<String, Object>> dataList = new ArrayList<>();

        for (int i = 0; i < dataNum; i++) {
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            JSONArray dataRow = dataArr.getJSONArray(i);

            for (int j = 0; j < fieldsNum; j++) {
                String headField = headList.get(j);
                String headCol = headMap.get(headField);

                map.put("Col_" + (j + 1), dataRow.getString(Integer.parseInt(headCol)));
            }
            dataList.add(map);
        }

        try {
            String path2 = KHExcelUtil.getInstance().exportExcel(dataList, headList, "E:\\", "excel2007导出-" + KHUtil.getIntRandom(6));
            System.out.println(path2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
